#include <ants.hpp>
#include <function.hpp>
#include <thread>

using namespace ants;

bool flags::consoled = false;
bool flags::debug = false;
int64_t flags::key = 0x1145141919810339ll;

Console::Console(int argc, const char **argv) {
    if(flags::consoled)
        throw std::runtime_error("Console already initialized");
    flags::consoled = true;
    this->arg = new args::args(argc, argv);
    this->view = nullptr;
    if(this->arg->hasArg("debug")) {
        flags::debug = true;
    }
}
const args::argitem &Console::getArgs(const std::string &title) const {
    return this->arg->getArg(title);
}
const std::vector<args::argitem> &Console::getArgs() const {
    std::vector<args::argitem> *args = new std::vector<args::argitem>();
    for(auto it : this->arg->getArgs()) {
        args->push_back(it.second);
    }
    return *args;
}
const std::vector<std::string> &Console::getTitles() const {
    std::vector<std::string> *args = new std::vector<std::string>();
    for(auto it : this->arg->getArgs()) {
        args->push_back(it.second.getTitle());
    }
    return *args;
}
size_t Console::getArgCount() const {
    return this->arg->getArgCount();
}
Console::~Console() {
    delete this->arg;
}
void Console::run() {
    if(!flags::consoled)
        throw std::runtime_error("Console not initialized");
    if(this->view == nullptr)
        throw std::runtime_error("TView not initialized");
    this->view->onLoad(flags::key);
    std::thread TViewMainLoop(&TView::loop, this->view);
    TViewMainLoop.join();
    this->view->onUnload(flags::key);
}
void Console::setView(TView *view) {
    this->view = view;
}